package com.jinbiao.quasar.scheduling.scheduled.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.jinbiao.quasar.model.SysJob;
import com.jinbiao.quasar.service.JobService;
import org.noear.solon.Solon;
import org.noear.solon.Utils;
import org.noear.solon.core.BeanBuilder;
import org.noear.solon.core.BeanExtractor;
import org.noear.solon.core.BeanWrap;
import com.jinbiao.quasar.scheduling.ScheduledAnno;
import com.jinbiao.quasar.scheduling.annotation.Scheduled;
import com.jinbiao.quasar.scheduling.scheduled.JobHandler;
import com.jinbiao.quasar.scheduling.scheduled.impl.JobBeanImpl;
import com.jinbiao.quasar.scheduling.scheduled.impl.JobMethodImpl;
import com.jinbiao.quasar.scheduling.scheduled.manager.IJobManager;
import com.jinbiao.quasar.scheduling.utils.ScheduledHelper;
import org.noear.solon.extend.sqltoy.annotation.Db;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;
import org.sagacity.sqltoy.model.EntityQuery;

import java.lang.reflect.Method;
import java.util.List;

/**
 * 任务提取器
 *
 * @author noear
 * @since 1.11
 * @since 2.2
 */
public class JobExtractor implements BeanBuilder<Scheduled>, BeanExtractor<Scheduled> {
    private final IJobManager jobManager;

    public JobExtractor(IJobManager jobManager) {
        this.jobManager = jobManager;
    }



    @Override
    public void doBuild(Class<?> clz, BeanWrap bw, Scheduled anno) throws Throwable {
        if (bw.raw() instanceof Runnable || bw.raw() instanceof JobHandler) {
            ScheduledAnno warpper = new ScheduledAnno(anno);
            SysJob job = null;
            if(StrUtil.isNotEmpty(warpper.name())) {
                JobService jobService = Solon.context().getBean(JobService.class);
                List<SysJob> jobList = jobService.findJobs(warpper.name());
                if(CollectionUtil.isNotEmpty(jobList)){
                    job = jobList.get(0);
                }
            }
            ScheduledHelper.configScheduled(warpper,job);
            JobHandler handler = new JobBeanImpl(bw);
            String name = warpper.name();
            if (Utils.isEmpty(name)) {
                name = bw.clz().getName();
            }

            jobManager.jobAdd(name, warpper, handler);
        } else {
            throw new IllegalStateException("Job only supports Runnable or JobHandler types!");
        }
    }

    @Override
    public void doExtract(BeanWrap bw, Method method, Scheduled anno) throws Throwable {
        ScheduledAnno warpper = new ScheduledAnno(anno);

        SysJob job = null;
        if(StrUtil.isNotEmpty(warpper.name())) {
            JobService jobService = Solon.context().getBean(JobService.class);
            List<SysJob> jobList = jobService.findJobs(warpper.name());
            if(CollectionUtil.isNotEmpty(jobList)){
                job = jobList.get(0);
            }
        }
        ScheduledHelper.configScheduled(warpper,job);

        JobHandler handler = new JobMethodImpl(bw, method);
        String name = warpper.name();
        if (Utils.isEmpty(name)) {
            name = bw.clz().getName() + "::" + method.getName();
        }

        jobManager.jobAdd(name, warpper, handler);
    }
}